﻿// -----------------------------------------------------------------------
// <copyright file="UserLogBehavior.cs" company="Accenture">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace NPSoft.Ferrero.Framework.Infrastructure.AOP
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.Unity.InterceptionExtension;
    using Microsoft.Practices.Unity;
    using NPSoft.Ferrero.Framework.Infrastructure.Crosscutting;
    using NPSoft.Ferrero.Framework.Utility;


    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class UserLogBehavior : IInterceptionBehavior
    {
        #region IInterceptionBehavior Members
        /// <summary>
        /// 获取当前行为需要拦截的对象类型接口。
        /// </summary>
        /// <returns>所有需要拦截的对象类型接口。</returns>
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        /// <summary>
        /// 通过实现此方法来拦截调用并执行所需的拦截行为。
        /// </summary>
        /// <param name="input">调用拦截目标时的输入信息。</param>
        /// <param name="getNext">通过行为链来获取下一个拦截行为的委托。</param>
        /// <returns>从拦截目标获得的返回信息。</returns>
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            var method = input.MethodBase;
            var key = method.Name;
            if (method.IsDefined(typeof(UserLogAttribute), false) || method.GetCustomAttributes(typeof(UserLogAttribute), false).ToString().Contains("UserLog"))
            {
                Console.WriteLine("Before Excute");
                IMethodReturn retvalue = getNext()(input, getNext);

                #region 参数部分
                Console.WriteLine("-------------Parameters Content---------------");
                for (int i = 0; i < input.Arguments.Count; i++)
                {
                    var parameter = input.Arguments[i];
                    Console.WriteLine(string.Format("NO.{0} Parameters:{1}", i + 1, parameter.ToString()));
                }
                Console.WriteLine("------------------------------------");
                #endregion

                #region 异常处理部分

                var ex = retvalue.Exception;

                if (ex == null)
                {
                    Console.WriteLine("Excute Success");
                }
                else
                {
                    //记录异常的内容 比如Log4Net等
                    Log4NetHandler.Error("\r\n" + "Exception Method： " + ex.TargetSite.Name + "\r\n" + "Exception Class： " +
                            ex.TargetSite.DeclaringType.FullName + "\r\n" + "Exception Message： " + ex.Message);
                    //retvalue.Exception设为null表示异常已经被处理过了
                    retvalue.Exception = null;
                }
                #endregion
                Console.WriteLine("Done");
                return retvalue;

            }

            return getNext().Invoke(input, getNext);
        }

        /// <summary>
        /// 获取一个<see cref="Boolean"/>值，该值表示当前拦截行为被调用时，是否真的需要执行
        /// 某些操作。
        /// </summary>
        public bool WillExecute
        {
            get { return true; }
        }

        #endregion
    }
}
